package cz.af.portletExams.exapando;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ClassNameLocalServiceUtil;
import com.liferay.portlet.expando.NoSuchTableException;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
import com.liferay.portlet.expando.model.ExpandoTable;
import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
import cz.af.portletExams.util.AFPortletUtil;

public class ExpandoApplicator {

    public static final String KOS_USERNAME = "kos-username";
    public static final String EXAPNDO_EXAM_USER = "exam_user";

    public void init() throws SystemException, PortalException {
        final long userClassNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());

        Company company = AFPortletUtil.getCurrentCompany();
        final long companyId = company.getCompanyId();

        try {
            ExpandoTableLocalServiceUtil.getTable(companyId, userClassNameId, EXAPNDO_EXAM_USER);
        } catch (NoSuchTableException e) {
            final ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.addTable(companyId, userClassNameId, EXAPNDO_EXAM_USER);
            ExpandoColumnLocalServiceUtil.addColumn(expandoTable.getTableId(), KOS_USERNAME, ExpandoColumnConstants.STRING);
        }
    }

}
